
# include<stdio.h>
# include<string.h>
# include<stdlib.h>
typedef enum Status{
    success,fail,fatal,range_error,ERROR
} Status;//success成功，fail失败，fatal致命的错误，range_error越界
typedef int ElemType;
typedef struct Node
{
    ElemType elem;
    struct Node *next;
} Node ,*Ptr;

typedef Ptr *Sqlistptr;
Status List_retrieve1(Sqlistptr L,int pos,Ptr *elem);
Status List_remove(Sqlistptr L,int pos);
Status List_init(Sqlistptr L);
Status List_creat(Sqlistptr L,ElemType *data,int length);
void List_print(Sqlistptr L);
void print(Status s);
int main(){
    Node head;
    head.elem=0;
    head.next=NULL;
    Ptr L;
    Status s;
    L = &head;
    L->next=NULL;
    //初始化测试
    s = List_init(&L);
    //创建链数据表
    int n=0;//人数
    int gap = 0;//间隔
    int last = 0;//剩余人数
    int pos=0;
    int b;
    int *data;
    b=scanf("%d %d %d",&n,&last,&gap);
    if(n<last||n<0||last<0||gap<0||b != 3){
        s = ERROR;
        print(s);
    }else{
     data = (int *)malloc(sizeof(int)*n);
    for (size_t i = 0; i < n; i++)
    {
        data[i] = i+1;
    }
    List_creat(&L,data,n);
    while (n != last)
    {
        pos=(pos + gap) % n;
        List_remove(&L,pos+1);
        n--;
    }
    List_print(&L);
    }

}
Status List_retrieve1(Sqlistptr L,int pos,Ptr *elem){
    Status s = range_error;
    Ptr p = (*L)->next;
    int i=1;
    while (i<pos && p)
    {
        i++;
        p=p->next;
    }
    if (i == pos && p)
    {
        s = success;
        *elem = p;
    }
    return s;
}
Status List_remove(Sqlistptr L,int pos){
    Status s =range_error;
    Ptr p,t;
    if (pos-1 > 0)
    {   
        s = List_retrieve1(L,pos-1,&p);
       if(s == success){
        t = p->next;
        p->next = t->next;
        free(t);
        t = NULL;
    }
    }else if(pos == 1)
    {
        t = (*L)->next;
        (*L)->next= t->next;
        free(t);
        s = success;
    }
    return s;
}
Status List_init(Sqlistptr L){
    Ptr p = (*L)->next;
    Ptr t;
    Status s = fail;
   while(p)
    {
        t = p;
        p = p->next;
        free(t);
    }
    if (p == NULL)
    {
        (*L)->next=NULL;
        s = success;
    }
    return s;
}
Status List_creat(Sqlistptr L,ElemType *data,int length){
    Status s =fail;
    Ptr p = NULL;
    s = List_init(L);
    if (s == success)
    {
       for (int i = length-1; i >= 0; i--)
       {
        p = (Ptr)malloc(sizeof(Node));
        if (p)
        {
            p->elem = data[i];
            p->next = (*L)->next;
            (*L)->next = p;
        }else{
            s = fail;
            break;
        }
       }
       
    }
    return s;
    
}
void print(Status i){
    switch (i)
    {
    case 0:
        printf("success\n");
        break;
    case 1:
        printf("fail\n");
        break;
    case 2:
        printf("fatal\n");
        break;
    case 3:
        printf("range_error\n");
        break;
    case 4:
        printf("ERROR");
        break;
    default:
        break;
    }
}
void List_print(Sqlistptr L){
    Ptr p = (*L)->next;
    if (p)
    {
         while (p&&p->next != NULL)
    {
       printf("%d,",p->elem);
       p=p->next;
    } 
    
    printf("%d\n",p->elem);
    }else{
        printf("0\n");
    }
    
}